package com.gzu.emr.controller;

import com.gzu.emr.dto.Patient;
import com.gzu.emr.service.PatientService;
import com.gzu.emr.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

import static com.gzu.emr.util.ResultGenerator.genFailResult;
import static com.gzu.emr.util.ResultGenerator.genSuccessResult;

@Controller
@RequestMapping("/patient")
public class PatientController {

    @Resource
    private PatientService patientService;

    @ResponseBody
    @RequestMapping(value = "/list/{status}", method = RequestMethod.GET)
    @Operation(description = "根据就诊状态查询列表")
    //@Parameters({@Parameter(name = "status", description = "就诊状态" ,required = true)})
    public Result list(@PathVariable String status) {
        if(status.length() != 1) {
            //error
            return genSuccessResult("error");
        }
        List<Patient> patients = patientService.listPatientByStatus(Integer.valueOf(status));
        return genSuccessResult(patients);
    }

    @ResponseBody
    @RequestMapping(value = "/search_name", method = RequestMethod.POST)
    @Operation(description = "根据患者姓名查询")
    @Parameters({@Parameter(name = "name", description = "患者姓名" ,required = true)})
    public Result searchByName(@RequestBody Map<String, Object> params) {
        var name = params.get("name");
        if(name == null) {
            return genFailResult("bad request body");
        }
        List<Patient> patients = patientService.searchPatientByName(name.toString());
        System.out.println(patients);
        return genSuccessResult(patients);
    }

}
